$(function() {
	$.divselect("#divselect_po_cate", "#orderCateId");
	
});

var initPubIptAndBtnEvent = function(){
	$("#publisherNameIpt").on('keypress',function(e){
		var key = e.which; //e.which按键的值
	    if (key == 13) {
	    	searchPublisherName();
	    }
	});
	
	$("#searchPublisherBtn").on('click',function(){
		searchPublisherName();
	});
	
};

var seletPublisherOfLI = function(obj){
	//取消所有选中的客户
	$("#publisherShwoMoreUl li").removeClass("active");
	$("#publisherShwoMoreUl li").removeClass("ready");
	$(obj).addClass("active");
	$(obj).addClass("ready");
};

var toSelectPublisher = function(){
	var fun1 = function(data){
		$("#loadPublisherDiv").html(data);
		initPubIptAndBtnEvent();
		$.box('.onlyuserforaddtaskalertbox', {
			onOpen : function() {
				$('#scrollbar').tinyscrollbar();
				$('#scrollbar_002').tinyscrollbar();//里面有2个滚动条*/			
			}
		}, {
			submit:{
				close:false,
				dom:['#submitPublisher'],
				fun:function(index){
					getSelectPublisher();
					this.close(index);
					
				}
			},
			close : {
				dom : [ '.onlyuserforaddtaskalertbox .closeforaddtask','.onlyuserforaddtaskalertbox .c_but_no' ]
			}
		});
	};
	
	ajaxCommFunWithDataType(globePath+"/publishMange/listPPBean"+action, "", 'text', fun1);
};

var searchPublisherName = function(){
	var paramsStr = "";
	var pubName = $("#publisherNameIpt").val();
	if(pubName != '请输入客户名'&&pubName != ''){
		paramsStr = "name="+pubName;
	}
	var fun = function(data){
		//子组织
		var pubHtml = getPubHmtl(data.listPPBean);
		$("#publisherShwoMoreUl").html(pubHtml);
	};
	ajaxCommFunWithDataType(globePath+"/publishMange/listByName"+action, paramsStr, 'json', fun);
};

//返回子组织html字符串
var getPubHmtl = function(publishers){
	var pubHtml = "";
	jQuery.each(publishers,function(index,item){
		pubHtml = pubHtml + "<li class=\"\" olid=\""+item.id+"\" onclick=\"seletPublisherOfLI(this);return false;\" >"+item.simpleName+"</li>";
		
		
	});
	return pubHtml;
};

var getSelectPublisher = function(){
	$("#publisherShwoMoreUl li").each(function(index,item){
		if($(item).hasClass("ready")&&$(item).hasClass("active")){
			$("#pubId").val($(item).attr("olid"));
			$("#pubName_div").html($(item).text());
		}
	});
};

var saveOrder = function(){
	var fun = function(msg) {
		if (msg.result == "success") {
			layer.msg("保存客户成功！",1,9,function(){
				window.location.href=globePath+"/ordermanage/toAddOrderDetail."+action+"?orderId="+$("#orderId").val();
			});
		} else {
			layer.alert(msg.result);
		};
	};
	
	var name = $.trim($("#name").val());
	if(name==''){
		layer.msg("书名不能为空");
		return false;
	}
	
	var simpleName = $.trim($("#pubId").val());
	if(simpleName==''){
		layer.msg("委托单位不能为空");
		return false;
	}
	var contactName = $.trim($("#startTime").val());
	if(contactName==''){
		layer.msg("来稿时间不能为空");
		return false;
	}
	
	var contactPhone = $.trim($("#endTime").val());
	if(contactPhone==''){
		layer.msg("取稿时间不能为空!");
		return false;
	}
	
	ajaxCommFunWithDataType(globePath+"/ordermanage/savePOrder"+action, $("#editOrderForm").serialize(),'json',fun);
};